<?php
namespace Sdk\Yqn;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class callYqnApiUtil
{
    private const URL = "https://hwc-open-api.yqn.com";

    private const KEY = "OPJxQDDNORrkEVtpxgngpjZEOnehldrz";

    public static function callYqnApi($action, $method, $data)
    {
        try {
            $client = new Client();
            $url = self::URL . $action;
            $headers = [
                'Content-Type' => 'application/json',
                'x-api-key' => self::KEY,
                'chraset' => 'UTF-8'
            ];
            if ('post' == $method) {
                $response = $client->request($method, $url, ['headers' => $headers, 'json' => $data]);
            } else {
                $response = $client->request($method, $url, ['headers' => $headers, 'query' => $data]);
            }
            $status = $response->getStatusCode();
            if (200 == $status) {
                $result = $response->getBody()->getContents();
                return json_decode($result, true);
            } else {
                return [];
            }
        } catch (ClientException $e) {
            echo $e->getMessage();
            return [];
        }
    }
}